package com.edu.neusoft.cookbook.controller;

import com.edu.neusoft.cookbook.common.ResponseResult;
import com.edu.neusoft.cookbook.entity.Recipes;
import com.edu.neusoft.cookbook.service.RecipesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 存储食谱信息的表(Recipes)表控制层
 *
 * @author makejava
 * @since 2024-12-07 11:35:06
 */
@RestController
@Slf4j
@Api(value = "食谱", tags = "食谱")
@RequestMapping("/api/recipes")
public class RecipesController{

    /**
     * 服务对象
     */
    @Autowired
    private RecipesService recipesService;

    /**
     * 分页查询食谱信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页查询食谱信息")
    @GetMapping
    public ResponseResult getByPage(@RequestParam int pageNum, @RequestParam int pageSize) {
        return ResponseResult.okResult(recipesService.findByPage(pageNum, pageSize));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据")
    @GetMapping("{id}")
    public ResponseResult selectOne(@PathVariable Integer id) {
        return ResponseResult.okResult(this.recipesService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param recipes 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增数据")
    @PostMapping
    public ResponseResult insert(@RequestBody Recipes recipes) {
        return ResponseResult.okResult(this.recipesService.save(recipes));
    }

    /**
     * 修改数据
     *
     * @param recipes 实体对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改数据")
    @PutMapping
    @Transactional
    public ResponseResult update(@RequestBody @Param("recipes") Recipes recipes) {
        return ResponseResult.okResult(this.recipesService.updateById(recipes));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public ResponseResult delete(@RequestParam("idList") List<Long> idList) {
        return ResponseResult.okResult(this.recipesService.removeByIds(idList));
    }

}

